package com.farm.code.server;

import java.util.ArrayList;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.time.TimeTool;

import org.apache.commons.beanutils.BeanUtils;
import com.farm.code.dao.FarmCodePackageDaoInter;
import com.farm.code.dao.FarmCodeTempletDaoInter;
import com.farm.code.domain.FarmCodePackage;
import com.farm.code.domain.FarmCodeTemplet;
import com.farm.code.server.FarmCodePackageManagerInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;

/**
 * 包信息
 * 
 * @author MAC_wd
 */
public class FarmCodePackageManagerImpl implements FarmCodePackageManagerInter {
	private FarmCodePackageDaoInter farmCodePackageDao;
	private FarmCodeTempletDaoInter farmCodeTempletDao;
//	private static final Logger log = Logger
//			.getLogger(FarmCodePackageManagerImpl.class);

	@Override
	public FarmCodePackage insertFarmCodePackageEntity(FarmCodePackage entity,
			LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setCusername(user.getName());
		entity.setEuser(user.getId());
		entity.setPackpath(entity.getPackcode());
		entity.setEusername(user.getName());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		return farmCodePackageDao.insertEntity(entity);
	}

	@Override
	public FarmCodePackage editFarmCodePackageEntity(FarmCodePackage entity,
			LoginUser user) {
		FarmCodePackage entity2 = farmCodePackageDao.getEntity(entity.getId());
		// entity2.setEuser(user.getId());
		// entity2.setEusername(user.getName());
		// entity2.setEtime(TimeTool.getTimeDate14());
		// sentity2.setPstate(entity.getPstate());
		entity2.setPcontent(entity.getPcontent());
		entity2.setPackcode(entity.getPackcode());
		// entity2.setPackpath(entity.getPackpath());
		entity2.setPackname(entity.getPackname());
		// entity2.setProid(entity.getProid());
		farmCodePackageDao.editEntity(entity2);
		return entity2;
	}

	@Override
	public void deleteFarmCodePackageEntity(String id, LoginUser user) {
		farmCodeTempletDao.deleteEntitys(DBRule.addRule(
				new ArrayList<DBRule>(), "PACKID", id, "="));
		farmCodePackageDao.deleteEntity(farmCodePackageDao.getEntity(id));
	}

	@Override
	public FarmCodePackage getFarmCodePackageEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return farmCodePackageDao.getEntity(id);
	}

	@Override
	public DataQuery createFarmCodePackageSimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery.init(query, "farm_code_package",
				"id,PSTATE,PCONTENT,PACKCODE,PACKPATH,PACKNAME,PROID");
		return dbQuery;
	}

	// ----------------------------------------------------------------------------------
	public FarmCodePackageDaoInter getfarmCodePackageDao() {
		return farmCodePackageDao;
	}

	public void setfarmCodePackageDao(FarmCodePackageDaoInter dao) {
		this.farmCodePackageDao = dao;
	}

	public FarmCodeTempletDaoInter getFarmCodeTempletDao() {
		return farmCodeTempletDao;
	}

	public void setFarmCodeTempletDao(FarmCodeTempletDaoInter farmCodeTempletDao) {
		this.farmCodeTempletDao = farmCodeTempletDao;
	}

	@Override
	public void copyPackAge(String projectId, String packAgeId, LoginUser user) {

		try {// 拷贝包
			FarmCodePackage pack = (FarmCodePackage) BeanUtils
					.cloneBean(getFarmCodePackageEntity(packAgeId));
			pack.setProid(projectId);
			pack = insertFarmCodePackageEntity(pack, user);
			// 拷贝包中的模板
			for (FarmCodeTemplet template : farmCodeTempletDao
					.selectEntitys(DBRule.addRule(new ArrayList<DBRule>(),
							"PACKID", packAgeId, "="))) {
				FarmCodeTemplet newTemplate = (FarmCodeTemplet) BeanUtils
						.cloneBean(template);
				newTemplate.setPackid(pack.getId());
				farmCodeTempletDao.insertEntity(newTemplate);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
